Ext.define('appfe.stockor.exec.form.ExecHoldEditFormController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.stockor.exec.form.ExecHoldEditForm',

    submitForm: function () {

        var myview = this.getView();
        //判断form是否处于有效状态, 即各个需要验证的field是否通过了验证
        if (!myview.getForm().isValid()) {
            Ext.MessageBox.alert('信息提示', '表单验证未通过, 请检查必填项');
            return;
        }
        var myviewmodel = myview.getViewModel();
        var isValidSlimsymbol = myviewmodel.get('isValidSlimsymbol');
        if (!isValidSlimsymbol) {
            Ext.MessageBox.alert('信息提示', '股票代码验证未通过, 请填写有效股票代码');
            return;
        }
        var parentItemId = myviewmodel.get('parentItemId');
        var mycontroller = myview.getController();

        myview.getForm().submit({
            clientValidation: true,
            submitEmptyText: false,
            disabled: true,
            waitMsg: '提交中...',
            url: BizHttpAction.upsertExecStockHold.url,
            method: BizHttpAction.upsertExecStockHold.method,
            params: {
                actionType: myviewmodel.get('actionType')
            },
            success: function (form, action) {
                Ext.getCmp(parentItemId).getController().search();
                mycontroller.closeWin();
                // Ext.MessageBox.alert('信息提示', '操作成功', function () {
                //     mycontroller.closeWin();
                //     Ext.getCmp(parentItemId).getController().search();
                // });
            },
            failure: function (form, action) {
                console.debug("action", action);
                var alertMsg;
                if (action.result && action.result.message) {
                    alertMsg = action.result.message;
                } else {
                    alertMsg = '操作失败, 请联系系统管理员'
                }
                Ext.Msg.alert('操作失败', alertMsg, function () {
                });
            }
        });

    },
    closeWin: function () {
        Ext.WindowManager.getActive().close();
    },
    /**
     * 失去焦点, trim文本及校验slimsymbol是否有效
     */
    trimTxtOnBlur: function (ths, event, eOpts) {
        var myviewmodel = this.getView().getViewModel();
        var slimsymbol = Ext.String.trim(ths.getValue())
        ths.setValue(slimsymbol);

        console.debug("slimsymbol", slimsymbol);

        re = /^(([s]{1}[z]{1}[30]{1})|([s]{1}[h]{1}[6]{1}))(\d{5})$/ //匹配股票代码正则表达式 
        if (re.test(slimsymbol)) {
            Ext.Ajax.request({
                url: BizHttpAction.queryStockBySlimSymbol.url + '/' + slimsymbol,
                method: BizHttpAction.queryStockBySlimSymbol.method,
                success: function (response, opts) {
                    var respJson = Ext.JSON.decode(response.responseText);
                    if (respJson.success != true) {
                        Ext.MessageBox.alert('提示', respJson.message);
                        return;
                    }
                    var stkName = respJson.data.name;
                    myviewmodel.set('holdStock.stkName', stkName);
                    myviewmodel.set('isValidSlimsymbol',true)
                },
                failure: function (response, opts) {
                    var failureMsg;
                    if (!Ext.String.startsWith(response.getResponseHeader("Content-type"), "application/json", true)) {
                        failureMsg = 'HTTP请求异常, 状态:'+response.status
                    } else {
                        var respJson = Ext.JSON.decode(response.responseText);
                        failureMsg = respJson.message;
                    }
                    Ext.MessageBox.alert('提示', failureMsg);
                    myviewmodel.set('holdStock.stkName', '');
                    myviewmodel.set('isValidSlimsymbol',false)

                }
            });
        } else {
            console.debug("不匹配股票代码正则表达式", slimsymbol);
            myviewmodel.set('isValidSlimsymbol',false)
        }
    }
});